package generadorDescriptores.dom;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class XmlFile {

	String path; 
	Document document; 
	String fullName; 
	
	public XmlFile(String path){
		this.path = path;
	}
	
	public void load(){
		File file = new File(this.path);
		fullName = file.getAbsolutePath().replace(".\\", "");
		
		DocumentBuilderFactory dFactory = new DocumentBuilderFactoryImpl();
		Document document = null;
		try {
			document = dFactory.newDocumentBuilder().parse(file);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		this.document = document;
	}
	
	public void setPath(String path){
		this.path = path; 
	}
	
	public Document getDocument(){
		return this.document;
	}
	
	
	public static void serialize(Document doc, String path){
		OutputFormat format = new OutputFormat(doc);
		format.setIndenting(true);
		format.setIndent(4);
		format.setStandalone(false);
		format.setEncoding("iso-8859-1");
		FileOutputStream file = null;
		try {
			file = new FileOutputStream(path);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		XMLSerializer serializer = new XMLSerializer(file, format);
		try {
			serializer.serialize(doc);
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			try {
				file.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public String getFullName(){
		return this.fullName;
	}
	
	
	
	
}

